home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / B-C / C++ FAQ Reference 1.0 / C++ FAQ Reference 1.0.rsrc / TEXT_530.txt < prev    next >
Encoding:
Text File  |  1993-06-30  |  1.5 KB  |  7 lines

  1. The advantage of using friends is generally syntactic.  Ie: both a member fn and a friend are equally privileged (100% vested), but a friend function can be called like f(obj), where a member is called like obj.f().  When it's not for syntactic reasons (which is not a 'bad' reason -- making an abstraction's syntax more readable lowers maintenance costs!), friends are used when two or more classes are designed to be more tightly coupled than you want for 'joe public' (ex: you want to allow class 'ListIter' to have more privilege with class 'List' than you want to give to 'main()').
  2.  
  3. Friends have three disadvantages.  The first disadvantage is that they add to the global namespace.  In contrast, the namespace of member functions is buried within the class, reducing the chance for namespace collisions for functions.
  4.  
  5. The second disadvantage is that they aren't inherited.  That is, the 'friendship privilege' isn't inherited.  This is actually an advantage when it comes to encapsulation.  Ex: I may declare you as my friend, but that doesn't mean I trust your kids.
  6.  
  7. The third disadvantage is that they don't bind dynamically.  Ie: they don't respond to polymorphism.  There are no virtual friends; if you need one, have a friend call a hidden (usually 'protected:') virtual member fn.  Friends that take a ptr/ref to a class can also take a ptr/ref to a publically derived class object, so they act as if they are inherited, but the friendship *rights* are not inherited (the friend of a base has no special access to a class derived from that base).